import { isFunction } from "lodash";

const lifecycleMthods=['onLaunch','onShow','onHide'];

class App{
    constructor(moduleInfo,openInfo){
        this.moduleInfo = moduleInfo;
        this.openInfo = openInfo;
        this.init();
    }

    init(){
        //初始化生命周期函数
        this.initLifecycle();
        //调用生命周期函数
        this.callLifecycle();
    }
    //初始化生命周期函数
    initLifecycle(){
        lifecycleMthods.forEach(name => {
            if(!isFunction(this.moduleInfo[name])){
                return false;
            }
            //bind指向当前this
            this[name]= this.moduleInfo[name].bind(this);
            
        });
    }
    //调用生命周期函数
    callLifecycle(){
        const {scene,pagePath,query}=this.openInfo;
        const options={
            scene,
            path:pagePath,
            query,
        }
        this.onLaunch(options);
        this.onShow(options);
    }
    //调用onShow
    callShowLifecycle(){
        const {scene,pagePath,query}=this.openInfo;
        const options={
            scene,
            path:pagePath,
            query,
        }
        this.onShow(options);

    }
    //调用onHide
    callHideLifecycle(){

        this.onHide();
    }

}
export {App};